#include <stdio.h>
#include <stdlib.h>//for malloc() and free()

#include "queue.h" 

typedef struct element_s {
	LIST_ENTRY(element_s) entries;
	int data;
	char buffer[32];
} element_t;

int main()
{
	element_t *p = NULL;
	element_t *curr = NULL;
	int i = 0;
	int mallocCount = 0;

	//定义队列
	LIST_HEAD(list_s, element_s) listHead;

	//队列初始化
	LIST_INIT(&listHead);

	for(i = 0; i < 5; i++)	
	{
		//插入
		p = NULL;
		p = malloc(sizeof(element_t));
		mallocCount ++;
		p->data = i;
		sprintf(p->buffer, "bufdata[%d]", i);
		LIST_INSERT_HEAD(&listHead, p, entries); 
	}

	//遍历，输出数据
	LIST_FOREACH(curr, &listHead, entries) {
		printf("%d %s\n", curr->data, curr->buffer);
	}   
	//遍历，输出数据
	LIST_FOREACH(curr, &listHead, entries) {
		if(curr->data == 1)
			printf("%d %s\n", curr->data, curr->buffer);
	}   

	//遍历，释放内存
        while( ((curr) = LIST_FIRST(&listHead)) != NULL )
	{
		LIST_REMOVE(curr, entries);	
		free(curr);
		mallocCount --;
		printf("mallocCount = [%d]\n", mallocCount);
	}
	printf("free end! mallocCount = [%d]\n", mallocCount );
	
	//遍历，输出数据
	LIST_FOREACH(curr, &listHead, entries) {
		printf("%d %s\n", curr->data, curr->buffer);
	}   
	exit(EXIT_SUCCESS);
}

